const express = require('express')
const app = express()
const morgan = require('morgan')
const cors = require('cors')

require('./models')
const router = require('./router')
app.use(express.json())
app.use(morgan('dev'))
app.use(cors())

// 路由规则中间件
app.use('/todolist', router)
// 404中间件
app.use((req, res, next) => {
  res.status(404).send('404 Page Not Found')
})
// 错误统一处理中间件
app.use((err, req, res, next) => {
  res.status(500).json({
    error: err.message
  })
})
const PORT = process.env.PORT || 9527

app.listen(PORT, () => {
  console.log(`Server is running at http://localhost:${PORT}`)
})